package com.droidnova.android.games.vortex;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.egl.EGLConfig;

import android.content.Context;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.Toast;
	//public class VortexRenderer implements GLSurfaceView.Renderer {
		public class VortexRenderer	extends GLSurfaceView implements Renderer
		{
		//private float _red = 0.9f;
	   // private float _green = 0.2f;
	   // private float _blue = 0.2f;
		private float _red = 0.01f;
		private float _green = 0.01f;
		private float _blue = 0.02f;	
	   /** axis instance */
		private axis axis3d;
		/** points instance */
		private points pointcube;
		private float z = -5.0f;			//Depth Into The Screen

		/** Is light enabled */
		private boolean light = false;
		/** Is blending enabled ( NEW ) */
		private boolean blend = false;
		
		private points1 pointcube1;
		
		private lines linecube;
		/** Angle For The axis */
		private float raxis; 	
		/** Angle For The points */
		private float rpoints; 
		int width , height;
		/* Rotation values */
		private float xrot;					//X Rotation
		


		float yrot;					//Y Rotation

		/* Rotation speed values */
		private float xspeed;				//X Rotation Speed
		private float yspeed;				//Y Rotation Speed
		/* Variables and factor for the input handler */
		private float oldX;
	    private float oldY;
		private final float TOUCH_SCALE = 0.2f;			//Proved to be good for normal rotation
		/** The Activity Context */
		private Context context;
		
		float _pcolors[] , _pvertices[];
		int _noVertices ;
		

		public VortexRenderer(Context context , points1 pointcube2) {	
		super(context);
		
			//Set this as Renderer
			this.setRenderer(this);
			//Request focus, otherwise buttons won't react
			this.requestFocus();
			this.setFocusableInTouchMode(true);
			
			//
			this.context = context;	
			
			pointcube1 = pointcube2;
			
			
			axis3d = new axis();
			
			
			
			
		}
		

		@Override
		 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		   //Really Nice Perspective Calculations
				gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); 
		 }
		 
		    @Override
		    public void onSurfaceChanged(GL10 gl, int w, int h) {
		        width = w;
		        height = h;
		        Log.v("Width Height", "w h -> " + w + h);
		    	if(h == 0) { 						//Prevent A Divide By Zero By
					h = 1;                          //Making Height Equal One  
		    	}
		    	gl.glViewport(0, 0, w, h);         //Reset The Current Viewport
		    	//Calculate The Aspect Ratio Of The Window
		    	gl.glMatrixMode(GL10.GL_PROJECTION); 	//Select The Projection Matrix
		    	gl.glLoadIdentity(); 
		    	
				GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
				gl.glMatrixMode(GL10.GL_MODELVIEW); 	//Select The Modelview Matrix
				gl.glLoadIdentity(); 					//Reset The Modelview Matrix
		    }
	    
	
	    
		@Override
	    public void onDrawFrame(GL10 gl ) {
			 	
	    	//float aspect_ratio = (float) width / height;
	    	//GLU.gluPerspective(gl, 70, aspect_ratio, 1, 100);
	    	// first clear the screen of any previous drawing
	        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
	        gl.glClearColor(_red, _green, _blue, 1.0f);
	     
	        // clear the color buffer to show the ClearColor we called above...
	        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	        
	        gl.glLoadIdentity();
	        
	      
	        gl.glTranslatef(0.0f, 0.0f, z);	 //Move z units into the screen
	     
	      //Rotate around the axis based on the rotation matrix (rotation, x, y, z)
			gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);	//X
			gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);	//Y
			
			gl.glEnable(GL10.GL_BLEND);
			gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

	        pointcube1.draw(gl);
	        
	     
	    	
	    	//Change rotation factors
			xrot += xspeed;
			yrot += yspeed;
	    }
	    
	    
	  
		
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			//
			float x = event.getX();
	        float y = event.getY();
	        Log.v("x","x"+x);
	        Log.v("y","y"+y);
	        //If a touch is moved on the screen
	        if(event.getAction() == MotionEvent.ACTION_MOVE) {
	        	//Calculate the change
	        	float dx = x - oldX;
		        float dy = y - oldY;
		        Log.v("dx","dx"+dx);
		        Log.v("dy","dy"+dy);
		        
	        	//Define an upper area of 10% on the screen
	        	int upperArea = this.getHeight() / 10;
	        	Log.v("touch","touch");
	        	//Zoom in/out if the touch move has been made in the upper
	        	if(y < upperArea) {
	        		Log.v("upper","upper");
	        		z -= dx * TOUCH_SCALE / 2;
	        		Log.v("z","z"+z);
	        	//Rotate around the axis otherwise
	        	} else {  
	        		Log.v("rot","rot");
	    	        xrot += dy * TOUCH_SCALE;
	    	        yrot += dx * TOUCH_SCALE;
	        	}        
	        }
	        //Remember the values
	        oldX = x;
	        oldY = y;
	        
	        //We handled the event
			return true;
		}
		
		public void setPointcube(points1 pointcube3) {
		       pointcube1 = pointcube3;
		    }
		
		 
		 
		 
	
	}
	
	
	

